<?php

declare(strict_types=1);

namespace App\Amqp\Consumer;

use Hyperf\Amqp\Annotation\Consumer;
use Hyperf\Amqp\Message\ConsumerMessage;
use Hyperf\Amqp\Result;
use PhpAmqpLib\Message\AMQPMessage;
use App\Model\Chat;

#[Consumer(exchange: 'chat001', routingKey: 'chat001', queue: 'chat001', name: "ChatConsumer", nums: 1)]
class ChatConsumer extends ConsumerMessage
{
	public function consumeMessage($data, AMQPMessage $message): Result
	{
		$post = json_decode($data, true);
		if (json_last_error() === JSON_ERROR_NONE) {
			$chat = new Chat();
			$chat->content = $post['content'];
			$chat->userid = $post['userid'];
			$chat->friendid = $post['friendid'];
			$res = $chat->save();
			if ($res) {
				return Result::ACK;
			} else {
				var_dump("发送消息保存失败");
				return Result::NACK;
			}
		} else {
			var_dump("发送消息json解析失败");
			return Result::NACK;
		}
	}
}
